package com.mrx.springboot.demo.filter;

import com.mrx.springboot.demo.utils.TracerUtils;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;

/**
 * @author Mr.X
 * @since 2025/7/4 20:52
 */
@Service
public class TraceIdFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(
            @NonNull HttpServletRequest request, @NonNull HttpServletResponse response,
            FilterChain filterChain
    ) throws ServletException, IOException {
        try {
            TracerUtils.startTracer();
            filterChain.doFilter(request, response);
        } finally {
            TracerUtils.stopTracer();
        }
    }
}
